const express =require('express')
const Users=require('../../Schema/userSchema')
const Roles=require('../../Schema/roleSchema')
const Logs=require('../../Schema/logSchema')
const jwt =require('jsonwebtoken')
const router= express.Router()


// 登录接口
router.post('/login',async(req,res)=>{
   console.log("调用接口成功")
   const userName=req.body.userName
   const passWord=req.body.passWord
   console.log(req.body)
  //  登录验证
   let result= await Users.findOne({userName:userName,passWord:passWord})
   if (!result) {
     console.log("查无此人")
     res.send({status:0})
     return;
   }else{
     let payload={
       id:result.id,
       userName:result.userName,
       passWord:result.passWord
     }
     jwt.sign(payload,"secret",{expiresIn:60*60*24},(err,token)=>{
          if (err) {
           throw err;
          }
          console.log(token)
          return  res.send({status:1,token:token})
     })
     console.log("查到了"+result)
   }

  
})

// 注册接口
router.post('/register',async(req,res)=>{
  const userName=req.body.userName
  const passWord=req.body.passWord
  let result= await Users.findOne({userName:userName,passWord:passWord})
  if (result) {
    console.log('账号已存在')
    res.send({status:0})
    return
  }
   const newUser= new Users({
    userName:userName,
    passWord:passWord
   })
   newUser.save().then((res)=>{
          console.log(res)
   })
   const newRoles=new Roles({
    userName:userName
 })
 newRoles.save().then((res)=>{
    console.log(res)
 })
   res.send({status:1})
})
// 查询用户角色
router.get('/role/:name',async(req,res)=>{
   let name =req.params.name;
   let result = await Roles.findOne({userName:name})
   if (!result) {
     res.send({status:0,result:[]})
     return
   }
   res.send({status:1,result:result})
})

// 查询用户
router.post('/getserchroles',async(req,res)=>{
   let name =req.body.name;
   let rolesResult =await Roles.find({
     userName:{ $regex:'^'+name}
   })
   if (rolesResult) {
     res.send(rolesResult)
     return
   }
   res.send([])
})
// 重置密码
router.put("/resetPwd",async(req,res)=>{
     let userName= req.body.name
     const result =await Users.update({userName:userName},{passWord:123456})
     console.log(result)
     if (result.n==1) {
       res.send({status:1})
       return
     }
     res.send({status:0})
})
// 分页查询
router.get('/pagiNationRoles/:page/:size',async(req,res)=>{
    let page =req.params.page;
    let size =req.params.size;
    const result = await Roles.find({}).skip(parseInt(page)*parseInt(size)).limit(parseInt(size))
    if (!result) {
      res.send({status:1,result:[]})
      return
    }
    res.send({status:0,result:result})
})
// 获取角色列表接口
router.get('/getroles',async(req,res)=>{
  let rolesResult=await Roles.find({})
  if (rolesResult) {
    res.send(rolesResult)
    return
  }
  let result =await Users.find()
  Array.from(result).forEach((item,index)=>{
    const newRoles=new Roles({
      userName:item.userName
   })
   newRoles.save().then((res)=>{
      console.log(res)
   })
  })

   res.send('查找成功')
})

// 添加登录角色信息到日志表中
router.post('/roles',async(req,res)=>{
  const userName=req.body.name
  const date=req.body.time
  const passWord=req.body.password
  const result= await Roles.findOne({userName:userName})
  if(!result){
    res.send({status:1})
    return
  }
  console.log(result)
  const newLogs= new Logs({
    userName:userName,
    passWord:passWord,
    role:result.role,
    date:date
  })
  newLogs.save().then((res=>{
    console.log(res)
  }))
  res.send({status:0})
})
// 编辑角色信息
router.put('/roles',async(req,res)=>{
  const userName=req.body.name
  const tel=req.body.tel
  const education=req.body.education
  const nation=req.body.nation
  const role=req.body.role
  const editForm={tel:tel,education:education,nation:nation,role:role}
  const result= await Roles.update({userName:userName},editForm)
  console.log(result)
  if (result) {
    res.send({status:1})
    return
  }
  res.send({status:0})
})
// 删除角色
router.delete('/roles/:name',async(req,res)=>{
  const userName=req.params.name
  console.log(userName)
  const result1 = await Users.remove({userName:userName})
  const result =await Roles.remove({userName:userName})
  console.log(result)
  if (result.n==1&&result1.n==1) {
    res.send({status:1})
    return
  }
  res.send({status:0})
})
module.exports=router